// pages/independent/course_list/course_list.js

import SortTool from "../../../utils/SortTool.js"
import CourseTool from "../../../utils/CourseTool.js"
Page({
	/**
	 * 页面的初始数据
	 */
	data:{
		_hot_down: "_hot_down",
		_hot_up:"_hot_up",
		courses_list:{},
		SignOfSort: true,			//true热度由高到低 false热度由低到高
        signOfTabBar: 0,            //0全部 1公开课 2定制课 3普通课
        tabBar:[
            {
                "name":"全部",
                "sign":0
            },
            {
                "name":"公开课",
                "sign":1
            },
            {
                "name":"定制课",
                "sign":2
            },
            {
                "name":"普通课",
                "sign":3
            }
		],
		pageNo: 1,
		isMine:"0"
	},
	
	onLoad(options) {
		console.log(options);
		let isMine = options.myclass;
		this.setData({
			isMine:isMine
		})
		console.log(this.data.isMine);
		if(this.data.isMine == "1"){
			let course_list = CourseTool.getMyCourse(0,1);
			course_list.then((res)=>{
				this.setData({
					courses_list:res.data
				})
				console.log(this.data.courses_list);
			})
		}
		else{
			let course_list = CourseTool.getCourseList(0,1);
			course_list.then((res)=>{
				this.setData({
					courses_list:res.data
				})
			})
		}
		
	},
	
	sortByHot(){
		if(this.data.SignOfSort){
			this.setData({
				['courses_list.records']: this.data.courses_list.records.sort(SortTool.sortByHot(1)),		//升序
				SignOfSort: false
			})
		}
		else{
			this.setData({
				['courses_list.records']: this.data.courses_list.records.sort(SortTool.sortByHot(-1)),	//降序
				SignOfSort: true
			})
		}
	},
    changeList(e) {
        let sign = e.currentTarget.dataset.sign;
        this.setData({
            signOfTabBar:sign
		})
		let course_list = CourseTool.getCourseList(sign,1);
		course_list.then((res)=>{
			this.setData({
				courses_list:res.data
			})
			console.log(this.data.courses_list);
		})
    },

    goToDetail(e){
		let id = e.currentTarget.dataset.courseid;
		let stars = e.currentTarget.dataset.stars;
        wx.navigateTo({
            url: '../course_detail/course_detail?Id='+id+"&stars="+stars,
        })
	},

	onPullDownRefresh() {
		wx.showNavigationBarLoading(); //显示导航加载进度
		// wx.startPullDownRefresh();
		console.log("下拉刷新触发");
		this.setData({
			pageNo: 1
		})
		if(this.data.isMine == "1"){
			let course_list = CourseTool.getMyCourse(0,1);
			course_list.then((res)=>{
				this.setData({
					courses_list:res.data
				})
				console.log(this.data.courses_list);
			})
		}
		else{
			let course_list = CourseTool.getCourseList(0,1);
			course_list.then((res)=>{
				this.setData({
					courses_list:res.data
				})
			})
		}
	},
	onReachBottom(){
		const _this = this;
		console.log("触底事件>>>");
		console.log((_this.data.pageNo-1)*10);
		console.log(_this.data.courses_list.total);
		if ((_this.data.pageNo-1)*10 <= _this.data.courses_list.total) {
			console.log(111);
			wx.showToast({
			  title: '加载中',
			  duration:"3000",
			  icon:"loading"
			})
			// if(this.data.isMine == "1"){
			// 	let course_list = CourseTool.getMyCourse(0,this.data.pageNo);
			// 	course_list.then((res)=>{
					
			// 		console.log(this.data.courses_list);
			// 	})
			// }
			// else{
			// 	let course_list = CourseTool.getCourseList(0,this.data.pageNo);
			// 	course_list.then((res)=>{
					
			// 		console.log(this.data.courses_list);
			// 	})
			// }
		}
	}
})